import processing.core.*; import krister.Ess.*; import processing.core.*; import java.awt.dnd.*; import java.awt.datatransfer.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class SPECTROGRAPH2 extends PApplet {

AudioChannel myChannel;
FFT myFFT;
int bands = 256; // Number of FFT frequency bands to calculate
int bufferSize;
int bufferDuration;
PImage dropimage;
int localPix = width*height;
int bandpos = width + 1;


public void setup() {
  frameRate(100);
  size(1024, 256);
  colorMode(HSB);
  Ess.start(this); // Start Ess
  //File loaded must be in the "data" folder
  myChannel = new AudioChannel("hello_dave.wav");
  bufferSize=myChannel.buffer.length;
  bufferDuration=myChannel.ms(bufferSize);
  myChannel.play(Ess.FOREVER);
  myFFT = new FFT(bands * 2); // We want 256 frequency bands, so we pass in 512
}

public void draw() {

  //background(176);

  // Get spectrum
  myFFT.getSpectrum(myChannel);


  // Draw FFT data
  stroke(255);
  loadPixels();
  
   
    //int j = 0;
      copy(0,0, width-1, height, 1,0, width, height);
      for(int i = 0; i < height ; i++) {
        float maxspect = max(0,5*myFFT.spectrum[i]*2048);
        println(maxspect);
        int spect = color (maxspect, 50+maxspect, maxspect);
        pixels[i*width] =  spect;        
      } 
      updatePixels();      
        
       
  
  //Ess.stop();
  /*
  while (i < height) {
   float y = height - pow(1024, (255.0 - i) / bands);
   float maxX = max(0, myFFT.maxSpectrum[i] * 2);
   float freX = max(0, myFFT.spectrum[i] * 2);
   stroke(255);
   stroke(0);
   for (int j = 0; j <bands;i++) {
   point(j, freX);
   }
   i++;
   }
   */
}

public void stop() {
  Ess.stop(); // When program stops, stop Ess too
  super.stop();
}


public void loadFile(String filename) {
  Ess.stop();
  Ess.start(this);
  myChannel = new AudioChannel(filename);
  //dropimage = loadImage(filename);
  println(filename);
  bufferSize=myChannel.buffer.length;
  bufferDuration=myChannel.ms(bufferSize);
  myChannel.play(Ess.FOREVER);
} 




 
DropTarget dt = new DropTarget(this, new DropTargetListener() {
  public void dragEnter(DropTargetDragEvent event) {
    // debug messages for diagnostics
    //System.out.println("dragEnter " + event);
    event.acceptDrag(DnDConstants.ACTION_COPY);
  }
 
  public void dragExit(DropTargetEvent event) {
    //System.out.println("dragExit " + event);
  }
 
  public void dragOver(DropTargetDragEvent event) {
    //System.out.println("dragOver " + event);
    event.acceptDrag(DnDConstants.ACTION_COPY);
  }
 
  public void dropActionChanged(DropTargetDragEvent event) {
    //System.out.println("dropActionChanged " + event);
  }
 
  public void drop(DropTargetDropEvent event) {
    //System.out.println("drop " + event);
    event.acceptDrop(DnDConstants.ACTION_COPY);
 
    Transferable transferable = event.getTransferable();
    DataFlavor flavors[] = transferable.getTransferDataFlavors();
    int successful = 0;
 
    for (int i = 0; i < flavors.length; i++) {
 try {
   Object stuff = transferable.getTransferData(flavors[i]);
   if (!(stuff instanceof java.util.List)) continue;
   java.util.List list = (java.util.List) stuff;
 
   for (int j = 0; j < list.size(); j++) {
     Object item = list.get(j);
     if (item instanceof File) {
  File file = (File) item;
 
       
  String filename = file.getPath();
  loadFile(filename);
  System.out.println(filename);
     }
   }
 
 }  
 catch (Exception e) {
   e.printStackTrace();
 }
    }
  }
}
); 

  static public void main(String args[]) {     PApplet.main(new String[] { "SPECTROGRAPH2" });  }}